import request from "../request.js"
import cfg from "../config.js"

let page_type = {
    open: function (page_info) {
        let page_html_id=page_info.page_html_id;
        let property=page_info.property;
        let ext_js=page_info.ext_js;
        let page_id=page_info.page_id;
        let rights=page_info.rights;
        let params=page_info.params;
        $("#" + page_html_id).addClass("html-builder-page row");
        $("#" + page_html_id).html("\
        <div class='col-md-6 col-sm-12 col-xs-12' style='padding:0.8rem;'>\
            <div class='config-part'></div>\
        </div>\
        <div class='col-md-6 col-sm-12 col-xs-12' style='padding:0.8rem;'>\
            <script>var view; $(function(){view=$('#" + page_html_id + " .view-part');})</script>\
            <div class='view-part form-view-part'></div>\
        </div>");

        let amisJSON = {
            //////////////////配置面板//////////////////
            "type": "page",
            "name": page_html_id + "_config",
            "body": [
                {
                    "type": "form",
                    "name": "config_form",
                    "api": cfg.api_server + "htmlbuilder/build",
                    "title": "通过HTML开发页面",
                    "resetAfterSubmit": true,
                    "redirect": "index.html", //?pid=${form_page_id}
                    "persistData": "amis-builder-page",
                    "clearPersistDataAfterSubmit": true,
                    "multiple": true,
                    "data": {
                        "api_server": cfg.api_server
                    },
                    "actions": [
                      {
                        "type": "reset",
                        "label": "重置"
                      },
                      {
                        "type": "button",
                        "level": "primary",
                        "label": "预览",
                        "onClick": function(a,b){
                            review();
                        }
                      },
                      {
                        "type": "submit",
                        "level": "primary",
                        "label": "生成页面"
                      }
                    ],
                    "controls": [
                        {
                            "type": "group",
                            "multiple": true,
                            "name": "config_group_1",
                            "body": [
                                {
                                    "label": "页面名称",
                                    "name": "name",
                                    "type": "input-text",
                                    "required": true,
                                    "clearable": true
                                },
                                {
                                    "label": "所属模块",
                                    "name": "module",
                                    "required": true,
                                    "type": "tree-select",
                                    "searchable": true,
                                    "onlyChildren": true,
                                    "showIcon": false,
                                    "source": {
                                        "method": "post",
                                        "url": "${api_server|url_decode}tree/build",
                                        "data": {
                                            "tree": [
                                                {
                                                    "label": "应用",
                                                    "label_field": "name",
                                                    "table_name": "sys_app",
                                                    "this_field": "id"
                                                },
                                                {
                                                    "label": "模块",
                                                    "label_field": "name",
                                                    "table_name": "sys_module",
                                                    "this_field": "id",
                                                    "parent_field": "app"
                                                }
                                            ]
                                        }
                                    }
                                },
                                {
                                    "label": "上级菜单",
                                    "name": "parent_menu",
                                    "required": true,
                                    "type": "tree-select",
                                    "searchable": true,
                                    "onlyChildren": true,
                                    "showIcon": false,
                                    "source": {
                                        "method": "post",
                                        "url": "${api_server|url_decode}tree/build",
                                        "data": {
                                            "tree": [
                                                {
                                                    "label": "菜单",
                                                    "label_field": "name",
                                                    "table_name": "sys_menu",
                                                    "this_field": "id",
                                                    "circle_parent_field": "parent_id"
                                                }
                                            ]
                                        }
                                    }
                                },
                                {
                                    "label": "赋权角色",
                                    "name": "roles",
                                    "required": true,
                                    "type": "select",
                                    "searchable": true,
                                    "onlyChildren": true,
                                    "multiple": true,
                                    "showIcon": false,
                                    "source": {
                                        "method": "post",
                                        "url": "${api_server|url_decode}tree/build",
                                        "data": {
                                            "tree": [
                                                {
                                                    "label": "角色",
                                                    "label_field": "name",
                                                    "table_name": "sys_role",
                                                    "this_field": "id"
                                                }
                                            ]
                                        }
                                    }
                                }
                            ]
                        },
                        {
                            "type": "group",
                            "multiple": true,
                            "name": "config_group_2",
                            "body": [
                                {
                                    "label": "功能描述",
                                    "name": "desc",
                                    "type": "input-text",
                                    "clearable": true
                                }
                            ]
                        },
                        {
                            "type": "html-editor",
                            "name": "html",
                            "size": "xxl",
                            "value": "<div>我的HTML页面</div>"
                        }
                    ]
                }
            ]
        }
        let amis = amisRequire('amis/embed');
        let config_amisScoped = amis.embed(
            '#' + page_html_id + " .config-part", amisJSON, {},
            {
                theme: 'antd'
            }
        );

        let review = function () {
            if ($('#' + page_html_id + " .view-part").length == 0) return;
            let data = config_amisScoped.getComponentByName(page_html_id + "_config.config_form").props.data;
            let html = data.html;

            $('#' + page_html_id + " .form-view-part").html(html);
        }

        review();
    }
}

export default page_type;